// pages/search/search.js
const app = getApp();
import {
  SearchModels
} from "../../utils/models/search/Search-models";
var searchModels = new SearchModels();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDel: false,
    searchVal: '',
    status: 0, //0初始化 1：无数据 2：有数据
    pageBackgroundColor: '#fff', //默认颜色
    more: [{
      attribute: null,
      cover: "https://www.allfighting.top/uploads/images/20200814/7c353d05e486952bde7c5c79d0a39b37.jpg",
      explain: "",
      hits: 179,
      id: 2,
      income: 0,
      income_max: 0,
      original_price: 50,
      price: 20,
      share: 0,
      sold: 0,
      stock: 100,
      title: "沙琪玛"
    },{
      attribute: null,
      cover: "https://www.allfighting.top/uploads/images/20200814/7c353d05e486952bde7c5c79d0a39b37.jpg",
      explain: "",
      hits: 179,
      id: 2,
      income: 0,
      income_max: 0,
      original_price: 50,
      price: 20,
      share: 0,
      sold: 0,
      stock: 100,
      title: "沙琪玛"
    },{
      attribute: null,
      cover: "https://www.allfighting.top/uploads/images/20200814/7c353d05e486952bde7c5c79d0a39b37.jpg",
      explain: "",
      hits: 179,
      id: 2,
      income: 0,
      income_max: 0,
      original_price: 50,
      price: 20,
      share: 0,
      sold: 0,
      stock: 100,
      title: "沙琪玛"
    },{
      attribute: null,
      cover: "https://www.allfighting.top/uploads/images/20200814/7c353d05e486952bde7c5c79d0a39b37.jpg",
      explain: "",
      hits: 179,
      id: 2,
      income: 0,
      income_max: 0,
      original_price: 50,
      price: 20,
      share: 0,
      sold: 0,
      stock: 100,
      title: "沙琪玛"
    }],
    productList: [{
      id: 1,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '11.80'
    }, {
      id: 2,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '12.80'
    }, {
      id: 3,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '13.80'
    }, {
      id: 4,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '14.80'
    }, {
      id: 5,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '15.80'
    }, {
      id: 6,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '16.80'
    }],
    shopList: [{
      id: 1,
      logo: '/images/icon/logo.png',
      title: '杰少按摩店',
      hits : 20
    },{
      id: 2,
      logo: '/images/icon/logo.png',
      title: '杰少按摩店',
      hits : 20
    },{
      id: 3,
      logo: '/images/icon/logo.png',
      title: '杰少按摩店',
      hits : 20
    }],
    history:[]
  },

  showDel: function () {
    this.setData({
      showDel: true
    });
  },

  hideDel: function () {
    this.setData({
      showDel: false
    });
  },

  // 获取输入的搜素内容
  searchVal: function (e) {
    var searchVal = e.detail.value;
    this.setData({
      searchVal: searchVal,
    });
  },

  // 删除搜索内容
  clearVal: function () {
    this.setData({
      searchVal: '',
    });

  },

  //点击完成 查询搜索内容 
  onSearch: function() {
    let searchVal = this.data.searchVal;
    let that = this;
    searchModels.search({keywords:searchVal},function(res){
      console.log(res);
      if(res.code > 0){
        var pageBackgroundColor = '#F8F8F9'
        that.setData({
          productList:res.data.goodsList,
          shopList: res.data.businessList,
          status:2,
          pageBackgroundColor: pageBackgroundColor
        })
      }else{
        var pageBackgroundColor = '#FFFFFF'
        that.setData({
          status:1,
          pageBackgroundColor: pageBackgroundColor
        })
      }
    })
    // 有数据改变背景色
    
    // 模拟搜索有数据
    // this.setData({
    //   status: 2
    // },function() {
    //   if(this.data.status == 2) {
    //     var pageBackgroundColor = '#F8F8F9'
    //   }else {
    //     var pageBackgroundColor = '#FFFFFF'
    //   }
      
      
    // });
    
  },

  // 删除历史搜索
  delKeyWord: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除历史搜索记录？',
      success (res) {
        if (res.confirm) {
          searchModels.delKeyWord(function(res){
            if(res.code > 0){
              that.setData({
                history:[]
              })
            }
          })
        }
      }
    })
  },
  getHistory:function(){
    var that = this;
    searchModels.getHistory(function(res){
      if(res.code > 0){
        that.setData({
          history:res.data
        })
      }
    })
  },
  recommend_list:function(){
    var that = this;
    searchModels.recommend_list(function (res) {
      if (res.code > 0) {
        that.setData({
          more:res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHistory();
    this.recommend_list();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})